﻿using Abp.Application.Services.Dto;
using gregn6Lib;
using Newtonsoft.Json;
using Sw.Hospital.HealthExaminationSystem.ApiProxy.Controllers;
using Sw.Hospital.HealthExaminationSystem.ApiProxy.CusReg;
using Sw.Hospital.HealthExaminationSystem.ApiProxy.CustomerReport;
using Sw.Hospital.HealthExaminationSystem.Application.Charge.Dto;
using Sw.Hospital.HealthExaminationSystem.Application.Common;
using Sw.Hospital.HealthExaminationSystem.Application.Common.Dto;
using Sw.Hospital.HealthExaminationSystem.Application.CusReg;
using Sw.Hospital.HealthExaminationSystem.Application.CusReg.Dto;
using Sw.Hospital.HealthExaminationSystem.Application.CustomerReport;
using Sw.Hospital.HealthExaminationSystem.Application.DoctorStation;
using Sw.Hospital.HealthExaminationSystem.Application.DoctorStation.Dto;
using Sw.Hospital.HealthExaminationSystem.Application.InspectionTotal;
using Sw.Hospital.HealthExaminationSystem.Application.InspectionTotal.Dto;
using Sw.Hospital.HealthExaminationSystem.Application.ItemInfo.Dto;
using Sw.Hospital.HealthExaminationSystem.Application.Picture.Dto;
using Sw.Hospital.HealthExaminationSystem.Common.Enums;
using Sw.Hospital.HealthExaminationSystem.Common.GlobalSources;
using Sw.Hospital.HealthExaminationSystem.Common.Helpers;
using Sw.Hospital.HealthExaminationSystem.Common.UserCache;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows;
using HealthExaminationSystem.Enumerations.Helpers;
using Sw.Hospital.HealthExaminationSystem.Application.GroupReport.Dto;
using Sw.Hospital.HealthExaminationSystem.Application.GroupReport;

namespace Sw.Hospital.HealthExaminationSystem.Comm
{

    public class HBTBPrintReport
    {
        private GridppReport ReportMain;
        IGroupReportAppService PrintPreviewAppService = new GroupReportAppService();

        /// <summary>
        /// 个人头像
        /// </summary>
        PictureDto url;
        public HBTBPrintReport()
        {
            ReportMain = new GridppReport();
        }
        /// <summary>
        /// 打印湖北团报
        /// </summary>
        /// <param name="isPreview">是否预览，是则预览，否则打印</param>
        public void Print(bool isPreview, EntityDto<Guid> input,string strBarPrintName, string path = "", List<Guid> cuslist = null)
        {
            GroupReportMainHBDto report = new GroupReportMainHBDto();
            HBReportInDto hBReportIn = new HBReportInDto();
            hBReportIn.Id = input.Id;
            hBReportIn.RegIds = cuslist;
            report = PrintPreviewAppService.GetGroupReportHB(hBReportIn);
            var GrdPath = GridppHelper.GetTemplate(strBarPrintName.Replace("\r\n", ""));
            if (!GrdPath.Contains(".grf"))
            {
                GrdPath = GrdPath + ".grf";
            }
            ReportMain.LoadFromURL(GrdPath);
            if (report.ZBBRS == null)
            {
                report.ZBBRS = new List<MasterRSDto>();
            }
           var reportJson = JsonConvert.SerializeObject(report.ZBBRS);
            var reportJsonz = "{\"Detail\":" + reportJson + "}";
            ReportMain.LoadDataFromXML(reportJsonz);

            if (ReportMain.ControlByName("人员情况统") != null)
            {
               var   Reportybjc = ReportMain.ControlByName("人员情况统").AsSubReport.Report;
                var reportJsonForExamineString = JsonConvert.SerializeObject(report.GZRS);
                var reportJsonry = "{\"Detail\":" + reportJsonForExamineString + "}";
                Reportybjc.LoadDataFromXML(reportJsonry);

            }
            if (ReportMain.ControlByName("目标疾病") != null)
            {
                var Reportybjc = ReportMain.ControlByName("目标疾病").AsSubReport.Report;
                var reportJsonForExamineString = JsonConvert.SerializeObject(report.MBJB);
                var reportJsonmb = "{\"Detail\":" + reportJsonForExamineString + "}";
                Reportybjc.LoadDataFromXML(reportJsonmb);

            }
            if (ReportMain.ControlByName("检查项目") != null)
            {
                var Reportybjc = ReportMain.ControlByName("检查项目").AsSubReport.Report;
                var reportJsonForExamineString = JsonConvert.SerializeObject(report.WHYSXM);
                var reportJsonjc = "{\"Detail\":" + reportJsonForExamineString + "}";
                Reportybjc.LoadDataFromXML(reportJsonjc);

            }
            if (ReportMain.ControlByName("异常指标汇总") != null)
            {
                var Reportybjc = ReportMain.ControlByName("异常指标汇总").AsSubReport.Report;
                var reportJsonForExamineString = JsonConvert.SerializeObject(report.YCXM);
                var reportJsonhz = "{\"Detail\":" + reportJsonForExamineString + "}";
                Reportybjc.LoadDataFromXML(reportJsonhz);

            }
            if (ReportMain.ControlByName("异常指标明细") != null)
            {
                var Reportybjc = ReportMain.ControlByName("异常指标明细").AsSubReport.Report;
                var reportJsonForExamineString = JsonConvert.SerializeObject(report.YCXMDetail);
                var reportJsonhz = "{\"Detail\":" + reportJsonForExamineString + "}";
                Reportybjc.LoadDataFromXML(reportJsonhz);

            }
            if (ReportMain.ControlByName("复查人员名单") != null)
            {
                var Reportybjc = ReportMain.ControlByName("复查人员名单").AsSubReport.Report;
                var reportJsonForExamineString = JsonConvert.SerializeObject(report.FCRS);
                var reportJsonhz = "{\"Detail\":" + reportJsonForExamineString + "}";
                Reportybjc.LoadDataFromXML(reportJsonhz);

            }
            if (ReportMain.ControlByName("检查结果") != null)
            {
                var Reportybjc = ReportMain.ControlByName("检查结果").AsSubReport.Report;
                var reportJsonForExamineString = JsonConvert.SerializeObject(report.JCJG);
                var reportJsonhz = "{\"Detail\":" + reportJsonForExamineString + "}";
                Reportybjc.LoadDataFromXML(reportJsonhz);

            }

            //打印机设置
            var printName = DefinedCacheHelper.GetBasicDictionaryByValue(BasicDictionaryType.PrintNameSet, 80)?.Remarks;

            if (!string.IsNullOrEmpty(printName))
            {
                ReportMain.Printer.PrinterName = printName;
            }
            try
            {
                if (isPreview)
                    ReportMain.PrintPreview(true);
                else
                {
                    if (path != "")
                    {
                        ReportMain.ExportDirect(GRExportType.gretPDF, path, false, false);

                    }
                    else
                        ReportMain.Print(false);
                }
            }
            catch (Exception ex)
            {

                throw;
            }


        }
    }
}
